/**
 * @author Fang Lu, fanglupku@gmail.com
 * 2011-4-12
 */
package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import dao.entity.APIEntity;
import dao.entity.QueryEntity;

public class QueryDao extends BaseDao{
	public static boolean insertQuery(Connection conn, QueryEntity query){
		String sqlString = "insert into query (api_id,result_count,search_engine,content) values(?,?,?,?)";
		PreparedStatement pstmt;
		try {
			pstmt = conn.prepareStatement(sqlString);
			pstmt.setInt(1, query.getApi_id());
			pstmt.setInt(2, query.getResult_count());
			pstmt.setInt(3, query.getSearch_engine());
			pstmt.setString(4, query.getContent());
			pstmt.executeUpdate();
			pstmt.close();
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}
	
	public static int insertQuery(Connection conn, ArrayList<QueryEntity> queryList){
		int okNum = 0;
		String sqlString = "insert into query (api_id,result_count,search_engine,content) values(?,?,?,?)";
		PreparedStatement pstmt;
		try {
			pstmt = conn.prepareStatement(sqlString);
			for (int i=0; i<queryList.size(); ++i){
				pstmt.setInt(1, queryList.get(i).getApi_id());
				pstmt.setInt(2, queryList.get(i).getResult_count());
				pstmt.setInt(3, queryList.get(i).getSearch_engine());
				pstmt.setString(4, queryList.get(i).getContent());
				try{
					pstmt.executeUpdate();
					++ okNum;
				} catch (SQLException e1){
					e1.printStackTrace();
				}
			}
			pstmt.close();
		} catch (SQLException e) {
			e.printStackTrace();
			return okNum;
		}
		return okNum;
	}
	
	public static ArrayList<QueryEntity> getQueryByAPIID(Connection conn, int API_ID){
		ArrayList<QueryEntity> resultList = new ArrayList<QueryEntity>();
		String sqlString = "select id,api_id,result_count,search_engine,content from query where api_id = "+API_ID;
		Statement stmt;
		try {
			stmt = conn.createStatement();
			ResultSet resultSet = null;
			resultSet = stmt.executeQuery(sqlString);
			while (resultSet.next()){
				int thisId = resultSet.getInt(1);
				int api_id = resultSet.getInt(2);
				int result_count = resultSet.getInt(3);
				int search_engine = resultSet.getInt(4);
				String content = resultSet.getString(5);
				QueryEntity thisEntity = new QueryEntity(thisId, api_id, result_count, search_engine, content);
				resultList.add(thisEntity);
			}
			resultSet.close();
			stmt.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return resultList;
	}
	
	public static ArrayList<QueryEntity> getAllQuery(Connection conn){
		ArrayList<QueryEntity> resultList = new ArrayList<QueryEntity>();
		String sqlString = "select id,api_id,result_count,search_engine,content from query";
		Statement stmt;
		try {
			stmt = conn.createStatement();
			ResultSet resultSet = null;
			resultSet = stmt.executeQuery(sqlString);
			while (resultSet.next()){
				int thisId = resultSet.getInt(1);
				int api_id = resultSet.getInt(2);
				int result_count = resultSet.getInt(3);
				int search_engine = resultSet.getInt(4);
				String content = resultSet.getString(5);
				QueryEntity thisEntity = new QueryEntity(thisId, api_id, result_count, search_engine, content);
				resultList.add(thisEntity);
			}
			resultSet.close();
			stmt.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return resultList;
	}
	public static void initQuery(){
		//part 2, insert all query
		Connection conn = getTestConn();
		ArrayList<APIEntity> apiList = APIDao.getAllAPI(conn);
		ArrayList<QueryEntity> queryList = new ArrayList<QueryEntity>();
		for (int i=0; i<apiList.size(); ++i){
			QueryEntity thisQuery = new QueryEntity(-1, apiList.get(i).getId(), 0, 0, "\"" + apiList.get(i).getName() + "\"+example+OR+java");
			queryList.add(thisQuery);
		}
		insertQuery(conn, queryList);
		try {
			conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static void main(String args[]){
		

	}
}
